package cs2340.spring13.team3.wheresmystuff.views;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import cs2340.spring13.team3.wheresmystuff.R;
import cs2340.spring13.team3.wheresmystuff.model.Item;
import cs2340.spring13.team3.wheresmystuff.model.Tag;
import cs2340.spring13.team3.wheresmystuff.model.User;
import cs2340.spring13.team3.wheresmystuff.presenters.LoginManager;

public class ViewSearchFilterActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_view_search_filter);

		final EditText tagString = (EditText) findViewById(R.id.tagSearchlist);

		// This might require we change the date attribute in Item to a "Date"
		// object
		@SuppressWarnings("unused")
		final EditText dateSearch = (EditText) findViewById(R.id.dateSearch);

		final CheckBox foundCheck = (CheckBox) findViewById(R.id.foundCheck);
		final CheckBox lostCheck = (CheckBox) findViewById(R.id.lostCheck);

		final Button searchButton = (Button) findViewById(R.id.searchButton);

		final User currentUser = LoginManager.getInstance().getUser();

		// On click of "Search" Button

		searchButton.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {

				// Don't know how to grab all available items
				List<Item> searchedItems = currentUser.getItems();
				ArrayList<Item> filteredItems = new ArrayList<Item>();

				// Grab tags from EditText entry
				String[] tagList = ((String) tagString.toString()).split(",? ");

				if (tagList != null) {
					for (Item item : searchedItems) {
						List<Tag> thisTag = item.getTags();
						for (String string : tagList) {
							if (thisTag.contains(string)) {
								filteredItems.add(item);
							}
						}
						searchedItems = filteredItems;
					}
				}

				// The below code filters out for lost or found items depending
				// on the checkboxes checked

				// filteredItems.clear();
				if (foundCheck.isChecked()) {
					for (Item item : searchedItems) {
						if (item.getFound()) {
							filteredItems.add(item);
						}
					}
					searchedItems = filteredItems;
				} else if (lostCheck.isChecked()) {
					for (Item item : searchedItems) {
						if (!item.getFound()) {
							filteredItems.add(item);
						}
					}
					searchedItems = filteredItems;
				} else {
					throw new RuntimeException("No items returned");
				}

				// filteredItems.clear();
				// String[] itemDate =
				// dateSearch.toString().split("?[0-1]?[0-9]/");
			}

		});

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.view_search_filter, menu);
		return true;
	}

}
